﻿using System;
using System.Xml.Serialization;

namespace QuickTuner
{
    /// <summary>
    /// Contains information for a single webradio station.
    /// </summary>
    [Serializable]
    [XmlRoot("Station")]
    public class Station : MarshalByRefObject
    {
        private string _title;
        private string _url;

        // properties _________________________________________________________

        /// <summary>
        /// Gets or sets title of the station as should be visible to the user.
        /// </summary>
        public virtual string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                OnTitleChanged(new EventArgs());
            }
        }

        /// <summary>
        /// Gets or sets listening URL of the webradio station.
        /// </summary>
        /// <remarks>
        /// This should be the URL to pass to the play engine to play this station.
        /// It should a URL that can be accessed with a normal web browser such as IE.
        /// </remarks>
        public virtual string StationURL
        {
            get { return _url; }
            set
            {
                _url = value;
                OnStationUrlChanged(new EventArgs());
            }
        }

        // events _____________________________________________________________

        public event EventHandler TitleChanged;
        public event EventHandler StationUrlChanged;

        protected virtual void OnTitleChanged(EventArgs e)
        {
            if (TitleChanged != null) TitleChanged(this, e);
        }

        protected virtual void OnStationUrlChanged(EventArgs e)
        {
            if (StationUrlChanged != null) StationUrlChanged(this, e);
        }

        // constructors _______________________________________________________

        public Station()
        {
            _title = string.Empty;
            _url = string.Empty;
        }

        // object overrides ___________________________________________________

        public override bool Equals(object obj)
        {
            var that = (Station)obj;

            var result = true;

            result &= (this.Title == that.Title);
            result &= (this.StationURL == that.StationURL);

            return result;
        }

        public override int GetHashCode()
        {
            // use a bitwise XOR of the title and url to differentiate
            var titleHash = (_title ?? string.Empty).GetHashCode();
            var urlHash = (_url ?? string.Empty).GetHashCode();

            return titleHash ^ urlHash;
        }

        public override string ToString()
        {
            return _title;
        }
    }
}